import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { MovieStatisticsEntity } from 'src/entities/statistics.entity';
import { Repository } from 'typeorm';

@Injectable()
export class StatisticsService {
  constructor(
    @InjectRepository(MovieStatisticsEntity)
    private readonly movieRepository: Repository<MovieStatisticsEntity>,
  ) {}
  getStatistics() {}
  getLikeStatistics() {}
  async putDownloadStatistics(movieId: number) {
    // 先判断有没有, 有就插入, 没有就创建
    const exist = await this.movieRepository.findOne({
      where: {
        movie_id: movieId,
      },
    });
    if (!exist) {
      await this.movieRepository.insert({
        movie_id: movieId,
        download_count: 1,
      });
    } else {
      return this.movieRepository.update(
        {
          movie_id: movieId,
        },
        {
          download_count: () => 'download_count + 1',
        },
      );
    }
  }
}
